
function inputScore(obj){
	var parent = $(obj).parents('.topic-item');
	var total = $(parent).find('.total-score');
	var inputs = $(parent).find('.subtopic-score');
	var totalScore = parseFloat(0);
	var ret = /^\d+(\.\d+)?$/;
	for(var i=0;i<inputs.length;i++){
		if(ret.test(inputs[i].value)){
			totalScore += parseFloat(inputs[i].value);
		}
	}
	$(total).text(totalScore);
}
function subTopicsWindow(id){
    	var openUrl = "subTopicList.jsp?id="+id;//弹出窗口的url
        var iHeight=window.screen.availHeight*0.8; //弹出窗口的宽度;
        var iWidth=window.screen.availWidth*0.9; //弹出窗口的高度;
        var iTop = (window.screen.availHeight-30-iHeight)/2; //获得窗口的垂直位置;
        var iLeft = (window.screen.availWidth-10-iWidth)/2; //获得窗口的水平位置;
        window.open(openUrl,"","height="+iHeight+", width="+iWidth+", top="+iTop+", left="+iLeft);
        return false
    }
function deleteTopic(obj){
	if(confirm("确定要移除该题目？")){
		$(obj).parents(".topic-item").remove();
	}
}
function addTopic(){
	var index = $(".topic-item").length+1;
	var id="topic-content"+index;
	var topicHtml = "<div class=\"col-sm-11 topic-item\">\r\n" + 
	"						<input type=\"hidden\" name=\"topoc_id\">\r\n" + 
	"						<div class=\"btn-group btn-group-sm pull-right\">\r\n" + 
	"						\r\n" + 
	"							<button onclick=\"subTopicsWindow('"+id+"')\" type=\"button\" class=\"btn btn-primary\" title=\"添加小题\"><span class=\"fa fa-plus\"></span></button>\r\n" + 
	"							<button onclick=\"deleteTopic(this)\" type=\"button\" class=\"btn btn-danger\" title=\"删除\"><span class=\"fa fa-close\"></span></button>\r\n" + 
	"						    <button type=\"button\" class=\"btn btn-default\" title=\"上移\"><span class=\"fa fa-arrow-up\"></span></button>&nbsp;\r\n" + 
	"						    <button type=\"button\" class=\"btn btn-default\" title=\"下移\"><span class=\"fa fa-arrow-down\"></span></button>&nbsp;\r\n" + 
	"						    \r\n" + 
	"						</div>\r\n" + 
	"						\r\n" +
	"						<div class=\"form-group\">\r\n" + 
	"							<label class=\"col-sm-1 control-label\">题干</label>\r\n" + 
	"							<div class=\"col-sm-9\">\r\n" + 
	"								<div class=\"input-group\">\r\n" + 
	"									<span class=\"input-group-addon\">"+index+"</span>\r\n" + 
	"									<input class=\"form-control\" type=\"text\" name=\"topic_title\" required/>\r\n" + 
	"								</div>\r\n" + 
	"							</div>\r\n" + 
	"						</div>\r\n" + 
	"						<div class=\"form-group\">\r\n" + 
	"							<label class=\"col-sm-1 control-label\">总分值:</label>\r\n" + 
	"							<div class=\"form-control-static total-score\" id=\"totalScore"+index+"\"></div>\r\n" + 
	"						</div>\r\n" + 
	"						\r\n" + 
	"						<div class=\"form-group\">\r\n" + 
	"							<div class=\"col-sm-offset-1 col-sm-9 topic-content\" id=\""+id+"\">\r\n" + 
	
	
	
									/*"<div class=\"subtopic-item\">"+
									"								<div class=\"input-group col-sm-2 pull-right\">\r\n" + 
									"									<span class=\"input-group-addon\">分值</span>\r\n" + 
									"									<input onkeyup=\"inputScore(this)\" class=\"form-control subtopic-score\" type=\"text\" name=\"subtopic_score\" required/>\r\n" + 
									"								</div>\r\n" +
	"								<!-- 小题ID隐藏域 -->\r\n" +
	"								<input type=\"hidden\" name=\"subtopic_id\">\r\n" + 
	"								\r\n" + 
	"								<!-- 小题内容 -->\r\n" + 
	"								<h4><span>1</span>.小题题目</h4>\r\n" + 
	"								<ul class=\"row list-style-none\">\r\n" + 
	"									<li class=\"col-sm-6\">\r\n" + 
	"										<label class=\"radio-inline\">\r\n" + 
	"											<input type=\"radio\" name=\"optionsRadiosinline\" id=\"\" value=\"\">A.选项 1\r\n" + 
	"										</label>\r\n" + 
	"									</li>\r\n" + 
	"									<li class=\"col-sm-6\">\r\n" + 
	"										<label class=\"radio-inline\">\r\n" + 
	"											<input type=\"radio\" name=\"optionsRadiosinline\" id=\"\" value=\"\">B.选项 1\r\n" + 
	"										</label>\r\n" + 
	"									</li>\r\n" + 
	"									<li class=\"col-sm-6\">\r\n" + 
	"										<label class=\"radio-inline\">\r\n" + 
	"											<input type=\"radio\" name=\"optionsRadiosinline\" id=\"\" value=\"\">C.选项 1\r\n" + 
	"										</label>\r\n" + 
	"									</li>\r\n" + 
	"									<li class=\"col-sm-6\">\r\n" + 
	"										<label class=\"radio-inline\">\r\n" + 
	"											<input type=\"radio\" name=\"optionsRadiosinline\" id=\"\" value=\"\">D.选项 1\r\n" + 
	"										</label>\r\n" + 
	"									</li>\r\n" + 
	"								</ul>\r\n" + 
									"</div>"+*/
	"							</div>\r\n" + 
	"						</div>\r\n" + 
	"					</div>";
	
	
	$("#topicContainer").append(topicHtml);
}
function leavePage(){
	if($('#submit_flag').val()==0)return '您输入的内容尚未保存，确定离开此页面吗？';
}
/* 下拉选择科目加载后台信息 */
$(function() {  
    $(".selectpicker").selectpicker({  
        noneSelectedText : '请选择'  
        });  
  
        $(window).on('load', function() {  
        $('.selectpicker').selectpicker('val', '');  
        $('.selectpicker').selectpicker('refresh');  
        });  
  
        // 下拉数据加载
    $.ajax({  
        type : 'get',  
        url :"subject/findAll",  
        dataType : 'json',  
        success : function(datas) {// 返回list数据并循环获取
            var select = $("#slpk");  
            for (var i = 0; i < datas.length; i++) {  
                select.append("<option value='"+datas[i].id+"'>"  
                        + datas[i].name + "</option>");  
            }  
            $('.selectpicker').selectpicker('val', '');  
            $('.selectpicker').selectpicker('refresh'); 
        }  
    });
});
/* 试卷信息提交保存 */
$("#form1").submit(function(envent){
	$('#submit_flag').val(1);
	
	var data = {};//传给后台的Paper对象
	/*private String title;
	private String description;
	private Subject subject;
	private CombinationType combType;
	private PaperType paperType;
	private Teacher teacher;//组卷教师
	private Integer time;   //考试时长，分钟
	private List<Topics> topics = new ArrayList<Topics>();
*/	
	var title = $("textarea[name='title']").val();
	var description = $("textarea[name='description']").val();
	var time = $("input[name='time']").val();
	var subjectId = $("#slpk").find("option:selected").val();
	var paperType = $("input[name='paperType']:checked").val();
	
	data = {
			"title":title,
			"description":description,
			"subject":{"id":subjectId},
			"combType":"Manual",
			"paperType":paperType,
			"time":time
			};
	
	/*private Long id;
	private Integer _index;
	private String title;
	private List<Subtopics> subtopics = new ArrayList<Subtopics>();*/
	var topics = new Array();//试卷大题Topics类
	var topicElements = $(".topic-item");
	
	
	for(var i=0;i<topicElements.length;i++){
		
		var topic_title_elem = $(topicElements[i]).find("input[name='topic_title']");
		var topic_title = topic_title_elem.val();
		var topic_index = $(topic_title_elem).prev().text();
		topics[i] = {
				"title":topic_title,
				"_index":topic_index
				};
		
		var subtopics = new Array();
		var subtopicElements = $(topicElements[i]).find('.subtopic-item');
		for(var j=0;j<subtopicElements.length;j++){
			/*Suptopics对象
			 * private Long id;
			private Integer _index;
			private Single single;
			private Multiple multiple;
			private Judgment judgment;
			private Explanation explanation;
			private float score;*/
			var _index = $(subtopicElements[j]).find('h4').find('span').text();
			var question_id = $(subtopicElements[j]).find("input[name='subtopic_id']").val();
			var subtopic_type = $(subtopicElements[j]).find("input[name='subtopic_type']").val();
			var score = $(subtopicElements[j]).find("input[name='subtopic_score']").val();
			
			subtopics[j] = {
					"_index":_index,
					"score":score
					};
			if(subtopic_type == 'Single'){
				$(subtopics[j]).attr("single",{"id":question_id});
			}else if(subtopic_type == 'Multiple'){
				$(subtopics[j]).attr("multiple",{"id":question_id});
			}else if(subtopic_type == 'Judgment'){
				$(subtopics[j]).attr("judgment",{"id":question_id});
			}else if(subtopic_type == 'Explanation'){
				$(subtopics[j]).attr("explanation",{"id":question_id});
			}else{}
		}
		$(topics[i]).attr("subtopics",subtopics);
		
	}
	$(data).attr("topics",topics);
	
	var result = JSON.stringify(data);//string类型
	
	$.ajax({
		url:'paper/save',
		type:'post',
		dataType:'json',
		data:{paper:result},
		success:function(data){
			alert(data.msg);
			if(data.success){
				window.location.href = "tch_paper.jsp";
			}
		},
		error:function(){
			$('#submit_flag').val(0);
			alert("出错!");
		}
	});
	return false;
});